package org.wltea.analyzer.sample;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Test {

	public static void main(String[] args) throws IOException{
		Map<String,String> natureMap =  new HashMap<String,String>();
		loadSogouDicNatures("/Users/zhengzb/Downloads/Freq/newSogouLabDic.dic",natureMap);
		loadOtherDicNatures("/Users/zhengzb/dev/workspace/ANSJAnalyzer/src/org/ansj/dic/systemLibrary.dic",natureMap);
		Set<String> keyset = natureMap.keySet();
		System.out.println("size:"+keyset.size());
		Map<Character,Set<String>> keywordMap = loadKeyword("/Users/zhengzb/dev/workspace/IKAnalyzer2012/src/org/wltea/analyzer/dic/main2012.dic",natureMap);
		File out = new File("/Users/zhengzb/dev/workspace/IKAnalyzer2012/src/org/wltea/analyzer/dic/main.dic");
		FileWriter fw = new FileWriter(out);
		Set<Character> keys = keywordMap.keySet();
		Set<String> entry = null;
		for(Character key : keys){
			entry = keywordMap.get(key);
			for(String line : entry){
				fw.write(line);
				fw.write("\r\n");
			}
		}
		fw.close();
	}
	
	private static Map<Character,Set<String>> loadKeyword(String filePath,Map<String,String> natureMap) throws IOException{
		FileReader fr = new FileReader(filePath);
		BufferedReader br = new BufferedReader(fr);
		String line = null;
		Map<Character,Set<String>> map = new HashMap<Character,Set<String>>();
		Set<String> entry = null;
		Character key = null;
		while((line=br.readLine())!=null){
			key = line.charAt(0);
			entry = map.get(key);
			if(entry==null) entry = new HashSet<String>();
			if(natureMap.get(line)==null)
				entry.add(line);
			else
				entry.add(line+" "+natureMap.get(line));
			map.put(key, entry);
		}
		br.close();
		fr.close();
		return map;
	}
	
	private static void loadOtherDicNatures(String filePath,Map<String,String> map) throws IOException{
		FileReader fr = new FileReader(filePath);
		BufferedReader br = new BufferedReader(fr);
		String line = null;
		String[] term;
		while((line=br.readLine())!=null){
			term = line.split("	");
			if(term!=null && term.length==3){
				term[0] = term[0].trim();
				term[1] = term[1].trim().toUpperCase();
				if(!map.containsKey(term[0]) && !term[1].equals("U") && !term[1].equals("Q") && !term[1].equals("AN") && !term[1].equals("NR") && !term[1].equals("NS") && !term[1].equals("VQ") ){
					if(term[1].startsWith("N") || term[1].equals("T")|| term[1].equals("JN")) term[1] = "N";
					if(term[1].equals("VN")) term[1] = "N,V";
					if(term[1].equals("I")) term[1] = "IDIOM";
					if(term[1].equals("J")) term[1] = "ABBR";
					if(term[1].equals("A") || term[1].equals("Z")) term[1] = "ADJ";
					if(term[1].equals("E") || term[1].equals("Y") || term[1].equals("O")) term[1] = "ECHO";
					if(term[1].startsWith("L") || term[1].equals("B")) term[1] = "LOCUTION";
					if(term[1].equals("C")) term[1] = "CONJ";
					if(term[1].equals("P")) term[1] = "PREP";
					if(term[1].equals("D")) term[1] = "ADV";
					if(term[1].equals("R") || term[1].equals("RR")) term[1] = "PRON";
					if(term[1].equals("F") || term[1].equals("S")) term[1] = "DIRECTION";
					if(term[1].startsWith("M")) term[1] = "CLAS";
					map.put(term[0], term[1]);
				}
			}
		}
		br.close();
		fr.close();
	}
	
	private static void loadSogouDicNatures(String filePath,Map<String,String> map) throws IOException{
		FileReader fr = new FileReader(filePath);
		BufferedReader br = new BufferedReader(fr);
		String line = null;
		String[] term;
		while((line=br.readLine())!=null){
			term = line.split("	");
			if(term!=null && term.length==3){
				map.put(term[0].trim(), term[2].trim());
			}
		}
		br.close();
		fr.close();
	}
}
